package com.projectbox.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.projectbox.model.Resource;
import com.projectbox.model.User;
import com.projectbox.service.ProjectService;
import com.projectbox.service.ResourceService;
import com.projectbox.service.SearchService;
import com.projectbox.service.UserService;


@Controller
@RequestMapping("/search")
public class SearchController extends MultiActionController {

		private Logger logger = LoggerFactory.getLogger(getClass());

		private UserService userService;
		private ProjectService projectService;
		private ResourceService resourceService;
		private SearchService searchService;
		
		
		public void setUserService(UserService userService) {
			this.userService = userService;
		}
		
		public void setSearchService(SearchService searchService) {
			this.searchService = searchService;
		}
		
		public void setResourceService(ResourceService resourceService){
			this.resourceService = resourceService;
		}
		
		@RequestMapping(method = RequestMethod.GET)
		public String displaySearchForm(Model model) {
			
			User user = userService.getCurrentUser();
			List<Resource> userResources = resourceService.findResourcesByUserId(user.getId());
		
			model.addAttribute("user", user);
			model.addAttribute("resources", userResources);
			return "search";
		}
		
		@RequestMapping(value = "/{pid}/delete", method = RequestMethod.GET)
		public void processDeleteProjectResources(@PathVariable("pid") Long pid,
				@RequestParam(value = "rid", required = true) Long rid,
				HttpServletResponse response) throws IOException {
			logger.debug("in the delete");
			Resource resource = this.resourceService.findResource(rid);
			this.resourceService.removeResource(resource);
		
			//return "searchResults";
		}
		
		@RequestMapping(value = "/doSearch", method = RequestMethod.GET)
		public String displaySearchResults(
				@RequestParam(value = "searchString", required = false) String searchString, 
				@RequestParam(value = "startDate", required = false) String startDate, 
				@RequestParam(value = "endDate", required = false) String endDate, 
				@RequestParam(value = "ckbox", required = false) String ckboxString,
				@RequestParam(value = "search-input", required = false) String topSearchString, Model model) {
			
			
			
			logger.debug("searching for: " + searchString);
			logger.debug("top string search " + topSearchString);
			
			User user = userService.getCurrentUser();
			
			model.addAttribute("user", user);
			
			if(ckboxString == null){
				logger.debug("top string search");
				
				model.addAttribute("searchString", topSearchString);
				model.addAttribute("resultProjectList", searchService.searchProjects(user.getId(), topSearchString.toLowerCase(), "1970-01-01", "1970-01-01"));
				return "searchResults";
			}
			
			model.addAttribute("searchString",searchString);
			if(ckboxString.equals("projects")){
				model.addAttribute("resultProjectList", searchService.searchProjects(user.getId(), searchString.toLowerCase(), startDate, endDate));
			}
			else if (ckboxString.equals("AllResources")){
				model.addAttribute("resultResourceList", searchService.searchResources(searchString.toLowerCase(), startDate, endDate));
			}
			
			else if (ckboxString.equals("myResources")){
				model.addAttribute("resultResourceList", searchService.searchMyResources(user.getId(),searchString.toLowerCase(), startDate, endDate));
			}
			
			else if (ckboxString.equals("resourceTypes")){
				model.addAttribute("resultResourceList", searchService.searchMyResources(user.getId(),searchString.toLowerCase(), startDate, endDate));
			}
			return "searchResults";
		}
}
